// 测试日总结功能的脚本

// 模拟浏览器环境
global.localStorage = {
  data: {},
  getItem: function(key) {
    return this.data[key] || null;
  },
  setItem: function(key, value) {
    this.data[key] = value;
  },
  removeItem: function(key) {
    delete this.data[key];
  }
};

// 测试日期格式
function testDateFormats() {
  const now = new Date();
  const date1 = now.toLocaleDateString();
  const date2 = now.toLocaleDateString('zh-CN');
  
  console.log('=== 日期格式测试 ===');
  console.log('toLocaleDateString():', date1);
  console.log('toLocaleDateString("zh-CN"):', date2);
  console.log('是否匹配:', date1 === date2);
  console.log('');
}

// 创建模拟历史记录
function createMockHistory() {
  const today = new Date().toLocaleDateString('zh-CN');
  const mockRecord = {
    date: today,
    time: '14:30:00',
    timestamp: Date.now(),
    total: 8,
    present: 6,
    leave: 1,
    absent: 1,
    late: 0,
    delay: 0,
    checker: '测试用户',
    type: 'dormitory',
    students: [
      { studentId: '2021001', name: '张三', dormitory: '101', status: 'present' },
      { studentId: '2021002', name: '李四', dormitory: '102', status: 'present' },
      { studentId: '2021003', name: '王五', dormitory: '103', status: 'leave' },
      { studentId: '2021004', name: '赵六', dormitory: '101', status: 'absent' },
      { studentId: '2021005', name: '钱七', dormitory: '102', status: 'present' },
      { studentId: '2021006', name: '孙八', dormitory: '104', status: 'present' },
      { studentId: '2021007', name: '周九', dormitory: '105', status: 'present' },
      { studentId: '2021008', name: '吴十', dormitory: '103', status: 'present' }
    ]
  };

  // 保存到本地存储
  const dormitoryHistory = JSON.parse(localStorage.getItem('dormitoryHistory') || '[]');
  dormitoryHistory.unshift(mockRecord);
  localStorage.setItem('dormitoryHistory', JSON.stringify(dormitoryHistory));

  console.log('=== 模拟历史记录创建 ===');
  console.log('已创建模拟记录:', mockRecord);
  console.log('');
}

// 测试日总结生成
function testDailySummary() {
  const dormitoryHistory = JSON.parse(localStorage.getItem('dormitoryHistory') || '[]');
  const today = new Date().toLocaleDateString('zh-CN');
  
  const todayRecords = dormitoryHistory.filter(record => record.date === today);
  
  console.log('=== 日总结测试 ===');
  console.log('当前日期:', today);
  console.log('今日记录数量:', todayRecords.length);
  
  if (todayRecords.length === 0) {
    console.log('错误: 今天没有查寝记录');
    console.log('历史记录中的日期:');
    dormitoryHistory.forEach(record => {
      console.log('-', record.date);
    });
  } else {
    const latestRecord = todayRecords[0];
    const summaryData = {
      type: 'daily',
      date: today,
      total: latestRecord.total,
      present: latestRecord.present,
      absent: latestRecord.absent,
      leave: latestRecord.leave,
      late: latestRecord.late,
      delay: latestRecord.delay || 0,
      leaveStudents: latestRecord.students.filter(s => s.status === 'leave')
    };

    console.log('日总结生成成功:');
    console.log(JSON.stringify(summaryData, null, 2));
  }
  console.log('');
}

// 检查本地存储
function checkLocalStorage() {
  const dormitoryHistory = localStorage.getItem('dormitoryHistory');
  const classHistory = localStorage.getItem('classHistory');
  
  console.log('=== 本地存储检查 ===');
  console.log('宿舍历史记录:');
  if (dormitoryHistory) {
    const records = JSON.parse(dormitoryHistory);
    console.log('记录数量:', records.length);
    records.slice(0, 3).forEach(record => {
      console.log(`- ${record.date} ${record.time} (${record.present}/${record.total})`);
    });
  } else {
    console.log('没有宿舍历史记录');
  }

  console.log('班级历史记录:');
  if (classHistory) {
    const records = JSON.parse(classHistory);
    console.log('记录数量:', records.length);
    records.slice(0, 3).forEach(record => {
      console.log(`- ${record.date} ${record.time} (${record.present}/${record.total})`);
    });
  } else {
    console.log('没有班级历史记录');
  }
  console.log('');
}

// 运行测试
testDateFormats();
createMockHistory();
testDailySummary();
checkLocalStorage();

console.log('=== 测试完成 ===');